package handler

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"server/basic/dao"
	"server/basic/model"
	"server/basic/proto/list"
)

type ListServer struct {
	list.UnimplementedListServerServer
}

func (s *ListServer) HospitalList(_ context.Context, req *list.HospitalListReq) (*list.HospitalListResp, error) {
	hosoital := []model.Hospitals{}
	if req.Hospital == 1 {
		m, err := dao.RedisHospitalListM()
		if err != nil {
			return nil, err
		}
		if m == "" {
			listM, err := dao.HospitalListM()
			if err != nil {
				return nil, err
			}
			hosoital = listM

			if dao.RedisHospitalListMAdd(listM) {
				return nil, errors.New("添加redis错误")
			}
		} else {
			json.Unmarshal([]byte(m), &hosoital)
		}
	} else {
		hospitalList, err := dao.RedisHospitalList(req.Page, req.PageSize)
		if err != nil {
			return nil, err
		}
		if hospitalList == "" {
			hospitals, err := dao.HospitalList(req.Page, req.PageSize)
			if err != nil {
				return nil, err
			}
			if err != nil {
				return nil, err
			}
			hosoital = hospitals

			if dao.RedisHospitalListAdd(hospitals, req.Page, req.PageSize) {
				return nil, errors.New("添加redis错误")
			}
		} else {
			json.Unmarshal([]byte(hospitalList), &hosoital)
		}
	}
	hositalList := []*list.HospitalList{}
	for _, hospitals := range hosoital {
		hositalList = append(hositalList, &list.HospitalList{
			Id:           int32(hospitals.Id),
			Url:          hospitals.Url,
			Address:      hospitals.Address,
			HospitalType: hospitals.HospitalType,
		})
	}
	return &list.HospitalListResp{List: hositalList}, nil
}
func (s *ListServer) DepartmentList(_ context.Context, req *list.DepartmentListReq) (*list.DepartmentListResp, error) {
	department := []model.Departments{}
	if req.Name == "" {
		departmentList, err := dao.DepartmentList(req.HospitalId)
		if err != nil {
			return nil, err
		}
		department = departmentList
	} else {
		where, err := dao.DepartmrntLisrWhere(req.Name, req.HospitalId)
		if err != nil {
			return nil, err
		}
		department = where
	}
	departmentList := []*list.DepartmentList{}
	for _, i2 := range department {
		departmentList = append(departmentList, &list.DepartmentList{
			Id:   int32(i2.Id),
			Name: i2.Name,
		})
	}
	return &list.DepartmentListResp{List: departmentList}, nil
}
func (s *ListServer) RegisterList(_ context.Context, req *list.RegisterListReq) (*list.RegisterListResp, error) {

	registerList, err := dao.RegisterList()
	if err != nil {
		return nil, err
	}
	Registerlist := []*list.RegisterList{}
	for _, i2 := range registerList {

		Registerlist = append(Registerlist, &list.RegisterList{
			DoctorId:  int32(i2.DoctorId),
			StartTime: fmt.Sprintf("%v", i2.StartTime),
			EndTime:   fmt.Sprintf("%v", i2.EndTime),
		})
	}
	return &list.RegisterListResp{List: Registerlist}, nil
}
func (s *ListServer) ThePatientList(_ context.Context, req *list.ThePatientListReq) (*list.ThePatientListResp, error) {
	patientList, err := dao.ThePatientList(req.UserId)
	if err != nil {
		return nil, err
	}
	departmentList := []*list.ThePatientList{}
	for _, i2 := range patientList {
		departmentList = append(departmentList, &list.ThePatientList{
			Name:    i2.Name,
			Content: i2.Content,
			Url:     i2.Url,
			Type:    i2.Type,
		})
	}
	return &list.ThePatientListResp{List: departmentList}, nil
}
func (s *ListServer) DoctorList(_ context.Context, req *list.DoctorListReq) (*list.DoctorListResp, error) {
	Doctorlist := []model.Doctors{}
	if req.DoctorType == 0 {
		doctorList, err := dao.DoctorList()
		if err != nil {
			return nil, err
		}
		Doctorlist = doctorList
	} else {
		doctorListType, err := dao.DoctorListtype(req.DoctorType)
		if err != nil {
			return nil, err
		}
		Doctorlist = doctorListType
	}

	DoctorList := []*list.DoctorList{}
	for _, i2 := range Doctorlist {
		DoctorList = append(DoctorList, &list.DoctorList{
			DepartmentId: int32(i2.DepartmentId),
			Name:         i2.Name,
			Url:          i2.Url,
			Position:     i2.Position,
		})
	}
	return &list.DoctorListResp{List: DoctorList}, nil
}
